package com.yjq.programmer.service.impl;

import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.MusicMapper;
import com.yjq.programmer.dao.RecentListenMapper;
import com.yjq.programmer.domain.Music;
import com.yjq.programmer.domain.RecentListen;
import com.yjq.programmer.domain.RecentListenExample;
import com.yjq.programmer.dto.MusicDTO;
import com.yjq.programmer.dto.RecentListenDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.dto.UserDTO;
import com.yjq.programmer.service.IRecentListenService;
import com.yjq.programmer.service.IUserService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-12-04 9:30
 */
@Service
@Transactional
public class RecentListenServiceImpl implements IRecentListenService {

    @Resource
    private RecentListenMapper recentListenMapper;

    @Resource
    private IUserService userService;

    @Resource
    private MusicMapper musicMapper;

    /**
     * 增加最近播放音乐数据
     * @param recentListenDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> addRecentListenMusic(RecentListenDTO recentListenDTO) {
        if(CommonUtil.isEmpty(recentListenDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 登录信息验证
        UserDTO userDTO = new UserDTO(recentListenDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        // 先删除原先的再插入最新的
        RecentListenExample recentListenExample = new RecentListenExample();
        recentListenExample.createCriteria().andUserIdEqualTo(userDTO.getId()).andMusicIdEqualTo(recentListenDTO.getMusicId());
        recentListenMapper.deleteByExample(recentListenExample);
        RecentListen recentListen = CopyUtil.copy(recentListenDTO, RecentListen.class);
        recentListen.setId(UuidUtil.getShortUuid());
        recentListen.setCreateTime(new Date());
        recentListen.setUserId(userDTO.getId());
        recentListenMapper.insertSelective(recentListen);
        return ResponseDTO.success(true);
    }

    /**
     * 获取最近10条音乐播放数据
     * @param recentListenDTO
     * @return
     */
    @Override
    public ResponseDTO<List<MusicDTO>> getRecentListenTenMusic(RecentListenDTO recentListenDTO) {
        // 登录信息验证
        UserDTO userDTO = new UserDTO(recentListenDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        RecentListenExample recentListenExample = new RecentListenExample();
        recentListenExample.createCriteria().andUserIdEqualTo(userDTO.getId());
        recentListenExample.setOrderByClause("create_time desc");
        List<RecentListen> recentListenList = recentListenMapper.selectByExample(recentListenExample);
        List<MusicDTO> musicDTOList = new ArrayList<>();
        for(RecentListen recentListen : recentListenList) {
            Music music = musicMapper.selectByPrimaryKey(recentListen.getMusicId());
            musicDTOList.add(CopyUtil.copy(music, MusicDTO.class));
        }
        return ResponseDTO.success(musicDTOList);
    }
}
